package icu.d4peng.cloud.gateway.controller;

import icu.d4peng.cloud.common.core.constants.ControllerConstant;
import icu.d4peng.cloud.gateway.service.DynamicRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @author d4peng
 * @version 1.0.0
 * @date 2022-01-04 17:17
 * @description GatewayController:网关控制器
 */
@RestController
@RequestMapping(ControllerConstant.GATEWAY_ROOT_PATH)
public class GatewayController {
    private final DynamicRouteService dynamicRouteService;

    @Autowired
    public GatewayController(DynamicRouteService dynamicRouteService) {
        this.dynamicRouteService = dynamicRouteService;
    }

    @GetMapping
    public Flux<RouteDefinition> routes() {
        return this.dynamicRouteService.routes();
    }

    @PostMapping
    public Mono<Void> save(@RequestBody RouteDefinition routeDefinition) {
        return this.dynamicRouteService.save(Mono.just(routeDefinition));
    }

    @DeleteMapping("/{id}")
    public Mono<Void> delete(@PathVariable("id") String id) {
        return this.dynamicRouteService.delete(Mono.just(id));
    }

}
